#!/bin/bash

# 小说爬虫框架 - Linux/macOS 安装脚本

echo ""
echo "========================================"
echo "   🕷️ 小说爬虫框架 - 自动安装脚本"
echo "========================================"
echo ""

# 检查Python环境
echo "📋 检查Python环境..."
if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
    echo "❌ 错误：未找到Python环境！"
    echo "💡 请先安装Python 3.7或更高版本"
    echo "🔗 参考：https://www.python.org/downloads/"
    exit 1
fi

# 确定Python命令
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
    PIP_CMD="pip3"
else
    PYTHON_CMD="python"
    PIP_CMD="pip"
fi

$PYTHON_CMD --version
echo "✅ Python环境检查通过"
echo ""

# 检查pip
echo "📦 检查pip..."
if ! command -v $PIP_CMD &> /dev/null; then
    echo "❌ 错误：未找到pip！"
    echo "💡 请先安装pip"
    exit 1
fi
echo "✅ pip检查通过"
echo ""

# 安装依赖
echo "📦 安装依赖包..."
echo "正在安装，请稍候..."

# 尝试使用清华镜像
if $PIP_CMD install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/; then
    echo "✅ 依赖安装完成（使用清华镜像）"
else
    echo "⚠️ 清华镜像安装失败，尝试默认源..."
    if $PIP_CMD install -r requirements.txt; then
        echo "✅ 依赖安装完成（使用默认源）"
    else
        echo "❌ 依赖安装失败！"
        echo "💡 请检查网络连接或手动安装依赖"
        exit 1
    fi
fi
echo ""

# 创建必要目录
echo "📁 创建必要目录..."
mkdir -p data/novels
mkdir -p data/chapters  
mkdir -p logs
echo "✅ 目录创建完成"
echo ""

# 设置执行权限
echo "🔧 设置执行权限..."
chmod +x run.py
echo "✅ 权限设置完成"
echo ""

echo "🎉 安装完成！"
echo ""
echo "📖 使用方法："
echo "  $PYTHON_CMD run.py --basic        # 运行基础示例"
echo "  $PYTHON_CMD run.py --novel        # 运行小说框架示例"  
echo "  $PYTHON_CMD run.py --help-guide   # 查看详细指南"
echo ""
echo "💡 更多信息请查看 README.md 文件"
echo "" 